$(document).ready(function(){
	$("#pic").ready(function() {
		$('.player').css({'visibility': 'visible'});
		$(".center").fadeIn();
	});

	var audio = document.getElementById('music');

	var loadMusic = function(){
		$.ajax({
			type: "GET",
			cache: false,
			dataType: 'jsonp',
			jsonp: 'callback',
			async: false,
			url: 'fm.php?callback=?',
			success: function(data){
				console.log(data)
				$.ajax({
					type: "GET",
					cache: false,
					dataType: 'jsonp',
					jsonp: 'callback',
					async: false,
					url: 'http://www.xiami.com/song/playlist/id/' + data.song_id +'/object_name/collect/object_id/' + data.song_id + '/cat/json?_ksTS=1&callback=?',
					success: function(ddata){
						if(!ddata.data.trackList[0]){
							loadMusic();
							return false;
						}
						audioPlay(ddata.data.trackList[0]);
					}
				});
			},
			error: function(){
				return false;
			}
		});
	}

	var audioPlay = function(data){
		audio.setAttribute("src", xiami(data.location));
		audio.addEventListener('play', playEvent, false);
		audio.addEventListener('pause', stopEvent, false);
		audio.addEventListener('timeupdate', updateProgress, false);
		audio.addEventListener('ended', loadMusic, false);
		audio.addEventListener('error', errorEvent, false);
		console.log('SongTitle: ' + data.title + ', SongId: ' + data.song_id);
		$('.album img').attr({'src': data.album_pic, 'alt': data.artist, 'data-id': data.song_id});
		$('.title h1').html(data.title);
		$('.title h2').html(data.artist);
		audio.play();
	}
	loadMusic();
	
	var updateProgress = function(){
		$('body').width() > 422 ? $('.progress .current').css({'width': audio.currentTime / audio.duration * 100 + '%'}) + ($('.album img').css('opacity') != 1 ? $('.album img').css({'opacity': 1}) : '') : $('.album img').css({'opacity': 1.1 - audio.currentTime / audio.duration});
	}
	
	var playEvent = function(){
		$('.album').addClass('playing');
		$('.progress').animate({opacity:"1"});
		$('.start i').addClass('playing').removeClass('fa-play').addClass('fa-pause');
		isPlaying = true;
	}

	var stopEvent = function(){
		$('.album').removeClass('playing');
		$('.start i').removeClass('playing').removeClass('fa-pause').addClass('fa-play');
		isPlaying = false;
	}

	var errorEvent = function(){
		loadMusic();
	}

	var xiami = function(sourceString) {
		var _loc9 = Number(sourceString.charAt(0));
		var _loc7 = sourceString.substring(1);
		var _loc5 = Math.floor(_loc7.length / _loc9);
		var _loc6 = _loc7.length % _loc9;
		var _loc2 = new Array();
		for (var _loc3 = 0; _loc3 < _loc6; ++_loc3) {
			if (_loc2[_loc3] == undefined) {
				_loc2[_loc3] = "";
			}
			_loc2[_loc3] = _loc7.substr((_loc5 + 1) * _loc3, _loc5 + 1);
		}
		for (var _loc3 = _loc6; _loc3 < _loc9; ++_loc3) {
			_loc2[_loc3] = _loc7.substr(_loc5 * (_loc3 - _loc6) + (_loc5 + 1) * _loc6, _loc5);
		}
		var _loc4 = "";
		for (var _loc3 = 0; _loc3 < _loc2[0].length; ++_loc3) {
			for (var _loc1 = 0; _loc1 < _loc2.length; ++_loc1) {
				_loc4 = _loc4 + _loc2[_loc1].charAt(_loc3);
			}
		}
		_loc4 = unescape(_loc4);
		var _loc8 = "";
		for (var _loc3 = 0; _loc3 < _loc4.length; ++_loc3) {
			if (_loc4.charAt(_loc3) == "^") {
				_loc8 = _loc8 + "0";
				continue;
			}
			_loc8 = _loc8 + _loc4.charAt(_loc3);
		}
		return (_loc8);
	}

	$('.center').click(function(){
		if ($('.start i').hasClass('playing')){
			audio.pause();
		} else {
			audio.play();
		}
	});

	$('.control .next').click(function(){
		audio.pause();
		loadMusic();
	})
	
	$('.home').click(function(){
		window.open('#');
	})
});